﻿Ext.util.Filter = Ext.extend(Object, {





    anyMatch: false,


    exactMatch: false,


    caseSensitive: false,



    constructor: function (config) {
        Ext.apply(this, config);



        this.filter = this.filter || this.filterFn;

        if (this.filter == undefined) {
            if (this.property == undefined || this.value == undefined) {




            } else {
                this.filter = this.createFilterFn();//创建筛选函数
            }

            this.filterFn = this.filter;
        }
    },

    //创建过滤函数
    createFilterFn: function () {
        var me = this,
            matcher = me.createValueMatcher(),//创建值筛选器
            property = me.property;

        return function (item) {
            return matcher.test(me.getRoot.call(me, item)[property]);
        };
    },


    getRoot: function (item) {
        return this.root == undefined ? item : item[this.root];
    },

    //创建值筛选器
    createValueMatcher: function () {
        var me = this,
            value = me.value,//用以筛选的条件值
            anyMatch = me.anyMatch,
            exactMatch = me.exactMatch,
            caseSensitive = me.caseSensitive,
            escapeRe = Ext.util.Format.escapeRegex; //通过使用字符串转义正则表达式

        if (!value.exec) {//如果value不是一个正则表达式
            value = String(value);

            if (anyMatch === true) {
                value = escapeRe(value);
            } else {
                value = '^' + escapeRe(value);
                if (exactMatch === true) {
                    value += '$';
                }
            }
            value = new RegExp(value, caseSensitive ? '' : 'i');
        }

        return value;
    }
});